<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Apply\Check;
use App\Models\Apply;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class ApplyController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '申请管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Apply());
        $grid->model()->orderBy('id', 'desc');
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->column(1/2, function ($filter) {
                $filter->like('visitor.name', '访客姓名');
                $filter->like('staff.name', '被访者姓名');
            });
            $filter->column(1/2, function ($filter) {
                $filter->between('visit_date', '到访日期')->date();
                $filter->equal('status', '状态')->select(Apply::$_status);
            });
        });
        $grid->column('id', __('编号'));
        $grid->column('visitor.name', __('访客姓名'));
        $grid->column('staff.name', __('被访者姓名'));
        $grid->column('reason', __('访问事由'));
        $grid->column('partners', __('随行人员'));
        $grid->column('visit_date', __('到访日期'));
        $grid->column('visit_time', __('到访时间'));
        $grid->column('status', __('状态'))->using(Apply::$_status);
        $grid->column('created_at', __('创建时间'))->display(function($time) {
            return date('Y-m-d H:i:s', strtotime($time));
        });
        $grid->column('updated_at', __('修改时间'))->display(function($time) {
            return date('Y-m-d H:i:s', strtotime($time));
        });
        $grid->disableCreateButton();
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            $actions->disableEdit();
            $actions->add(new Check());
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Apply::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('visitor_id', __('Visitor id'));
        $show->field('staff_id', __('Staff id'));
        $show->field('reason', __('Reason'));
        $show->field('partners', __('Partners'));
        $show->field('visit_date', __('Visit date'));
        $show->field('visit_time', __('Visit time'));
        $show->field('status', __('Status'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Apply());

        $form->number('visitor_id', __('Visitor id'));
        $form->number('staff_id', __('Staff id'));
        $form->text('reason', __('Reason'));
        $form->text('partners', __('Partners'));
        $form->date('visit_date', __('Visit date'))->default(date('Y-m-d'));
        $form->text('visit_time', __('Visit time'));
        $form->switch('status', __('Status'))->default(1);

        return $form;
    }
}
